package com.example.servicea.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.SqlHelper;
import com.baomidou.mybatisplus.plugins.Page;
import com.example.servicea.dao.RoleMapper;
import com.example.servicea.dao.RolePermissionMapper;
import com.example.servicea.entity.RoleEntity;
import com.example.servicea.entity.RolePermissionEntity;
import com.example.servicea.service.RoleService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

/**
 * @Author generator.
 * @Date: 2017/10/26 10:58
 */
@Service("roleServiceImpl")
public class RoleServiceImpl implements RoleService {
	@Autowired
	private RoleMapper roleMapper;

    @Override
    public String insert(RoleEntity role){
        roleMapper.insert(role);
        return role.getId();
    }
    @Override
    public List<RoleEntity> selectList(RoleEntity role){
        EntityWrapper<RoleEntity> query = new EntityWrapper<RoleEntity>();
		return roleMapper.selectList(query);
	}

    @Override
    public Page selectPage(RoleEntity role, String page, String limit){
        EntityWrapper<RoleEntity> query = new EntityWrapper<RoleEntity>();
        if(StringUtils.isNotBlank(role.name)){
            query.like("name",role.name);
        }
        List<RoleEntity> list = roleMapper.selectPage(
                new Page<RoleEntity>(Integer.parseInt(page),Integer.parseInt(limit)),
                query);
        int total = roleMapper.selectCount(query);
        Page RoleEntityPage = new Page();
        return RoleEntityPage;
	}
    @Override
    public int selectCount(RoleEntity role){
        EntityWrapper<RoleEntity> query = new EntityWrapper<RoleEntity>();
        return roleMapper.selectCount(query);
	}

    @Override
    public RoleEntity selectOne(RoleEntity role){
        return roleMapper.selectOne(new RoleEntity());
	}

    @Override
    public RoleEntity selectById(String id){
        return roleMapper.selectById(id);
	}

	@Override
    @Transactional(rollbackFor = Exception.class)
    public String updateById(RoleEntity role){
        roleMapper.updateById(role);
        return role.getId();
	}

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteById(String id){
        return SqlHelper.retBool(roleMapper.deleteById(Integer.parseInt(id)));
	}

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteBatchIds(String ids){
        String[] s = ids.split(",");
        List<String> list = Arrays.asList(s);
        return SqlHelper.retBool(roleMapper.deleteBatchIds(list));
	}

    @Autowired
    private RolePermissionMapper rolePermissionMapper;
    @Override
    public boolean insertPermissions(String permissionIds0, String roleId) {
        String[] permissionIds = permissionIds0.split(",");
        if(permissionIds.length>0){
            rolePermissionMapper.delete(new EntityWrapper<RolePermissionEntity>().eq("roleId",roleId));
            for (int i = 0; i < permissionIds.length; i++) {
                RolePermissionEntity obj = new RolePermissionEntity();
                obj.setPermissionId(permissionIds[i]);
                obj.setRoleId(roleId);
                boolean res = SqlHelper.retBool(rolePermissionMapper.insert(obj));
                if(!res) {return false;}
            }
        }
        return true;
    }

}
